//index.js
//获取应用实例

var util = require('../../utils/util.js');

const app = getApp()


/* var tpoints=[{longitude: 113.699559,latitude: 34.780254}, 
  {longitude: 113.701855,latitude:34.779778}]
console.log(tpoints)
var point=[]

var x=113.699559
var y=34.780254

var a1={longitude:x,latitude:y}
point.push(a1)

 x=113.701855
 y=34.779778
var a2={longitude:x,latitude:y}
point.push(a2)
console.log(point) */

/* var shuzu=[]
shuzu[0]=[{name:"20"},{name:"队伍"},{name:"任务"},{name:"人数"},{name:"消防车"},{name:"水枪"},{name:"设备"},{name:"灭火器"}]
shuzu[1]=[{name:"编号41"},{name:"队伍44"},{name:"任74务"},{name:"人71数"},{name:"消防74车"},{name:"水枪"},{name:"特种设备"},{name:"风力灭火器"}]
shuzu[2]=[{name:"编4141号"},{name:"成都市干干消防支队锦江区大队规避路中队"},{name:"任71务"},{name:"人717数"},{name:"消71防车"},{name:"水枪"},{name:"特种设备"},{name:"风力灭火器"}]
shuzu[3]=[{name:"编号"},{name:"队伍"},{name:"任务"},{name:"人数"},{name:"消防车"},{name:"水枪"},{name:"特种设备"},{name:"风力灭火器"}]

var huoc=["中国","100","100","100"] */

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    longitude:113.324520,
    latitude:28,
    isShowConfirm:false,
    dateArray:[{name:"编号"},{name:"队伍"},{name:"任务"},{name:"人数"},{name:"消防车"},{name:"水枪"},{name:"设备"},{name:"灭火器"}],
    dateArray3:[{name:"火场地点："},{name:"经度:"},{name:"纬度："},{name:"天气："}],
   /*  polyline: [{
      points:point,
      color: "#ff6600",
      width: 20,
      dottedLine: false,
      arrowLine: true,
      borderColor:"#000",
      borderWidth:5
    }], */
  },

  onHide:function(){
    wx.requestSubscribeMessage({
      tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
    })
  },

  onUnload:function(){
    wx.requestSubscribeMessage({
      tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
    })
  },

  onLoad: function () {
    
    
    var day = util.formatTime(new Date());

    var value8=wx.getStorageSync('keytongzhi')
    if(Math.abs(value8.split('&')[1]-day)<2){this.setData({zhishijiushililang:value8.split('&')[0]})}

    var value9=wx.getStorageSync('xianhuancun')
    if(value9.split('&')[0]!=0){
      var sasasdsdasas = JSON.parse(value9.split('&')[0]);
      if(Math.abs(value9.split('&')[1]-day)<2){
        this.setData({
          polyline:[{
            points:sasasdsdasas,
            color: "#ff6600",
            width: 8,
            dottedLine: false,
            arrowLine: true,
            borderColor:"#000",
            borderWidth:5
          }]
        })
    }
    }

    var valuebiaoge=wx.getStorageSync('biaoge')
    if(valuebiaoge.split('&')[0]!=0){

      var strToObj11 = JSON.parse(valuebiaoge.split('&')[0]);
      var strToObj211 = JSON.parse(valuebiaoge.split('&')[1]);
      this.setData({
        dateArray4:strToObj211,
        dateArray2:strToObj11
      })

    }

    var value10=wx.getStorageSync('mianhuancun')
    if(value10.split('&')[0]!=0){
      var sasasasas = JSON.parse(value10.split('&')[0]);
      if(Math.abs(value10.split('&')[1]-day)<2){
        console.log(value10.split('&')[0])
        this.setData({
          polygon:[{
            points:sasasasas,
            fillColor: "#ff000050",
            strokeColor: "#ff0000",
            strokeWidth: 2,
            zIndex:1,
          }],
        })
      }
    }




    


    var value11=wx.getStorageSync('dianhuancun')
    if(value11.split('&')[0]!=0){
      var sasasasas = JSON.parse(value11.split('&')[0]);
      if(Math.abs(value11.split('&')[1]-day)<2){
        this.setData({
          markers:[{
            latitude:sasasasas[1],
            longitude:sasasasas[0]
          }],
        })
        }
    }

 

      
    
      

  

    wx.getSystemInfo({
      success: (res) => {
        this.setData({
          controls: [{
            id: 1,
            iconPath: '../../images/dingwei.jpg',
            position: {
              left: 20,
              top: res.windowHeight - 160,
              width: 50,
              height: 50
            },
            clickable: true
          },
          {
            id: 2,
            iconPath: '../../images/huo.jpg',
            position: {
              left: res.windowWidth-75,
              top: res.windowHeight - 180,
              width: 60,
              height: 60
            },
            clickable: true
          },
          {
            id: 3,
            iconPath: '../../images/ren.jpg',
            position: {
              left: res.windowWidth-75,
              top: res.windowHeight - 250,
              width: 60,
              height: 60
            },
            clickable: true
          },

          {
            id: 4,
            iconPath: '../../images/tongzhi.jpg',
            position: {
              left: res.windowWidth-75,
              top: res.windowHeight -550,
              width: 60,
              height: 60
            },
            clickable: true
          },
          {
            id: 5,
            iconPath: '../../images/jingao.jpg',
            position: {
              left: res.windowWidth-75,
              top: res.windowHeight -480,
              width: 60,
              height: 60
            },
            clickable: true
          },
          {
            id: 6,
            iconPath: '../../images/huodian.jpg',
            position: {
              left: res.windowWidth-75,
              top: res.windowHeight -410,
              width: 60,
              height: 60
            },
            clickable: true
          }

        ]
        })
      }
    })


  
        wx.connectSocket({
      url:'wss://dxlg.giserhub.com/wss',
      success:function(){console.log('hhh')},
      fail:function(){console.log('hsshh')}
    })

    wx.onSocketOpen(function (res) {
      console.log('WebSocket连接已打开！')

      wx.sendSocketMessage({
        data: 'game1',
      })
    })
    var that=this;
    wx.onSocketMessage(function (ress) {
      
      var linshishenfen;
      var tt=0;
      if(ress.data.split("&")[0]=="biaoge"){
        tt=1;
        var strToObj = JSON.parse(ress.data.split("&")[1]);
        var strToObj2 = JSON.parse(ress.data.split("&")[2]);
        that.setData({
          dateArray4:strToObj2,
          dateArray2:strToObj
        })
        wx.setStorageSync('biaoge', ress.data.split("&")[1]+'&'+ress.data.split("&")[2])
      }
      
      if(ress.data.split("&")[0]=="quanti"){
        tt=1;
        that.setData({
          zhishijiushililang:ress.data.split("&")[1],
          }
        )
        wx.setStorageSync('keytongzhi', ress.data.split("&")[1]+'&'+day)
        wx.showModal({
          title:'收到消息',
          content:ress.data.split("&")[1],
          success:function(res) {
           if(res.confirm) {
            console.log('用户点击确定')
           }
          }
         })
      }
      if(ress.data.split("&")[0]=="xiaofangyuan"||ress.data.split("&")[0]=="feixiaofangyuan"){
        tt=1;

        var value=wx.getStorageSync('key1')
        if(value==0){linshishenfen='xiaofangyuan'}
        if(value==1){linshishenfen='feixiaofangyuan'}
        if(ress.data.split("&")[0]==linshishenfen){
          that.setData({
            zhishijiushililang:ress.data.split("&")[1],
            }
          )
          wx.setStorageSync('keytongzhi', ress.data.split("&")[1]+'&'+day)
          console.log(linshishenfen)
          wx.showModal({
            title:'收到消息',
            content:ress.data.split("&")[1],
            success:function(res) {
             if(res.confirm) {
              console.log('用户点击确定')
             }
            }
           })
        }
        

      }


      if(ress.data.split("&")[0]=="dxm"){
        tt=1;
        var strToObj = JSON.parse(ress.data.split("&")[1]);

        if(strToObj.xian){
          var linshi = JSON.stringify(strToObj.xian) //使用四个空格缩进
          wx.setStorageSync('xianhuancun', linshi+'&'+day)
          wx.showModal({
            title:'收到火情预警，位置已在地图标出',
            content:'可移动地图查看',
           })
          that.setData({
            polyline:[{
              points:strToObj.xian,
              color: "#ff6600",
              width: 8,
              dottedLine: false,
              arrowLine: true,
              borderColor:"#000",
              borderWidth:5
            }],
          })
        }

        if(strToObj.mian){
          var linshi = JSON.stringify(strToObj.mian) //使用四个空格缩进
          wx.setStorageSync('mianhuancun', linshi+'&'+day)
          wx.showModal({
    
            title:'收到火情预警，位置已在地图标出',
           
            content:'可移动地图查看',
           
           
           })

          that.setData({
            polygon:[{
              points:strToObj.mian,
              fillColor: "#ff000050",
              strokeColor: "#ff0000",
              strokeWidth: 2,
              zIndex:1,
            }],
          })
        }

        if(strToObj.dian){

          var linshi = JSON.stringify(strToObj.dian) //使用四个空格缩进
          wx.setStorageSync('dianhuancun', linshi+'&'+day)
          console.log( strToObj.dian)
          that.setData({
            markers:[{
              latitude:strToObj.dian[1],
              longitude:strToObj.dian[0]
            }],
          })

          wx.getLocation({
            type: 'gcj02',
            success: function (res) {
    
              
              wx.showModal({
    
                title:'收到火情预警，位置已在地图标出',
               
                content:'你当前距火点位置为'+Math.sqrt(Math.pow(((Math.abs(strToObj.dian[0]-res.longitude)/0.00026969)*30),2)+ Math.pow(((Math.abs(strToObj.dian[1]-res.latitude)/0.00026969)*30),2))+'米',
               
                success:function(res) {
               
                 if(res.confirm) {
               
                  console.log('用户点击确定')
               
                 }
               
                }
               
               })
              
              
            }
        })

        }

      }
      

      if(tt==0){
        
/*         console.log('就是你'+ress.data)
        var t=true;
        if(ress.data=="game1"||ress.data=="game2"||ress.data=="success"||ress.data.split(",")[0]=="mamimamihong"){
             t=false
        }
        if(t){
          wx.showToast({
            title: '接受到无法处理的信息',
            icon: 'none',
            duration: 2000
          })
        } */

      }


    })

    

    wx.onSocketClose(function (res) {
      console.log('WebSocket连接已关闭！')
    })
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  onReady: function (e) {
    this.mapCtx = wx.createMapContext('map')
  },
  mapclick: function() {
    wx.requestSubscribeMessage({
      tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
    })
    var that = this;
    console.log("地图点击");
  },



  controltap(e) {
    var that=this;
    if(e.controlId==1){

      wx.requestSubscribeMessage({
        tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
      })

      wx.getLocation({
        type: 'gcj02',
        success: function (res) {
          that.setData({
            latitude: res.latitude,
            longitude:res.longitude,
          })
          that.moveTolocation();
        }
    })
    }

    if(e.controlId==2){

      wx.requestSubscribeMessage({
        tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
      })

      wx.showModal({
        title: '是否发现火点准备上报',
        content: '谎报，虚报将以严处',
        success: function (res) {
          if (res.confirm) {  
            wx.chooseLocation({
              success: function(ress){
                console.log(ress)
                // success
                wx.showModal({
                  title: '确定将火点上报？',
                  content: '谎报，虚报将以严处',
                  success: function (res) {
                    if (res.confirm) {  
                      wx.sendSocketMessage({
                        data: ress.latitude+','+ress.longitude+','+ress.address,
                      })
                    } else {   
                      console.log('点击取消回调')
                    }
                  }
                })
              },
              fail: function() {
                // fail
              },
              complete: function() {
                // complete
              }
            })
          } else {   
            console.log('点击取消回调')
          }
        }
      })


    }

    if(e.controlId==3){
      wx.navigateTo({
        url: '/pages/geren/geren',
      })
    }


    if(e.controlId==4){
      
      wx.requestSubscribeMessage({
        tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
      })
      that.setData({
        isShowConfirm:true
      })
    }

    if(e.controlId==5){
      wx.requestSubscribeMessage({
        tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
      })
      that.setData({
        istongzhi:true
      })
    }


    if(e.controlId==6){
      
      wx.requestSubscribeMessage({
        tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
      })
      var value11=wx.getStorageSync('dianhuancun')
      if(value11.split('&')[0]!=0){
        var sasasasas = JSON.parse(value11.split('&')[0]);
          that.setData({
            latitude: sasasasas[1],
            longitude:sasasasas[0]
          })
          that.moveTolocation();
      }


    }



  },

  cancelll:function(){
    wx.requestSubscribeMessage({
      tmplIds: ['KQO9Tw3hu-iMxAaxMWP4hFTiogM5eoMzpwqxV933KaM'],
    })
    this.setData({
      isShowConfirm:false,
      istongzhi:false
    })
  },
/**
 * 移动到中心点
 */
moveTolocation: function () {
  //mapId 就是你在 map 标签中定义的 id
  var mapCtx = wx.createMapContext(mapid,this);
  mapCtx.moveToLocation();
},

  huoqu: function(e) {
    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        console.log(res);
      },
      fail: function (res) {
        console.log(res);
      }
    })

  }
  
})


